package main;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.LazyOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import com.ccb.channel.convert.ConvertMapper;
import com.mongodb.hadoop.util.MongoConfigUtil;

public class Convert {
	public static void main(String... args) throws Exception {

		// String input="dedup_in\\p8\\p8.txt";
		String input = "dedup_in";
		String output = "dedup_out";
		runJob(input, output);
	}

	public static void runJob(String inpath, String outpath) throws Exception {

		Configuration conf = new Configuration();
		MongoConfigUtil.setOutputURI(conf, "mongodb://192.168.1.100/ccb.p8");
		Job job = Job.getInstance(conf, "convert");
		job.setJarByClass(Convert.class);
		job.setMapperClass(ConvertMapper.class);
		// job.setReducerClass(P8ReducerMongo.class);
		job.setNumReduceTasks(0);
		job.setInputFormatClass(TextInputFormat.class);
		// job.setOutputFormatClass(TextOutputFormat.class);
		LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
		// LazyOutputFormat.setOutputFormatClass(job, MongoOutputFormat.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);
		// job.setOutputValueClass(BSONWritable.class);
		FileInputFormat.addInputPath(job, new Path(inpath));
		Path opath = new Path(outpath);
		FileOutputFormat.setOutputPath(job, opath);
		FileSystem fs = (FileSystem) opath.getFileSystem(conf);
		fs.delete(opath, true);

		System.exit(job.waitForCompletion(true) ? 0 : 1);

	}
}
